package cn.edu.zafu.bigdata.x.zhouyuyan.work9;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentManagementSystem {
    private List<Student> students;
    private String filePath = "students.dat";

    public StudentManagementSystem(){
        students = new ArrayList<>();
        loadStudents();
    }

    private void loadStudents(){
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))){
            students = (List<Student>) ois.readObject();
        }catch (FileNotFoundException e){

        }catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }
    }

    private void saveStudents(){
        try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))){
            oos.writeObject(students);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    private void addStudent(){
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.print("请输入学号：");
            String id = scanner.nextLine();
            if(students.stream().anyMatch(student -> student.getId().equals(id))){
                System.out.println("学号重复，请重新输入。");
                continue;
            }
            System.out.print("请输入姓名：");
            String name = scanner.nextLine();
            System.out.print("请输入电话：");
            String phone = scanner.nextLine();

            Student student = new Student(id,name,phone);
            students.add(student);
            saveStudents();
            System.out.println("学生信息添加成功！");
            break;
        }
    }

    private void retrieveStudent(){
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.print("请输入学号检索学生信息（输入'QUIT'退出）");
            String input = scanner.nextLine();
            if(input.equalsIgnoreCase("QUIT")){
                break;
            }
            Student student = students.stream().filter(s -> s.getId().equals(input)).findFirst().orElse(null);
            if(student != null){
                System.out.println(student);
            }else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private void updateStudent(){
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.print("请输入学号更新学生信息（输入'QUIT'退出）：");
            String input = scanner.nextLine();
            if(input.equalsIgnoreCase("QUIT")){
                break;
            }
            Student student = students.stream().filter(s -> s.getId().equals(input)).findFirst().orElse(null);
            if(student != null){
                System.out.print("请输入新姓名：");
                String name = scanner.nextLine();
                System.out.print("请输入新电话：");
                String phone = scanner.nextLine();

                student.setName(name);
                student.setPhone(phone);
                saveStudents();
                System.out.println("学生信息更新成功！");
            }else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private void deleteStudent(){
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.print("请输入学号删除学生信息（输入'QUIT'退出）：");
            String input = scanner.nextLine();
            if(input.equalsIgnoreCase("QUIT")){
                break;
            }
            Student student =students.stream().filter(s -> s.getId().equals(input)).findFirst().orElse(null);
            if(student != null){
                students.remove(student);
                saveStudents();
                System.out.println("学生信息删除成功！");
            }else{
                System.out.println("未找到该学生。");
            }
        }
    }

    public static void main(String[] args) {
        StudentManagementSystem system = new StudentManagementSystem();
        Scanner scanner = new Scanner(System.in);

        while(true){
            System.out.println("\n======= 学生信息管理系统 =======");
            System.out.println("1.添加学生信息");
            System.out.println("2.检索学生信息");
            System.out.println("3.更新学生信息");
            System.out.println("4.删除学生信息");
            System.out.println("5.退出程序");
            System.out.print("请输入操作序号（1-5）：");
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice){
                case 1:
                    system.addStudent();
                    break;
                case 2:
                    system.retrieveStudent();
                    break;
                case 3:
                    system.updateStudent();
                    break;
                case 4:
                    system.deleteStudent();
                    break;
                case 5:
                    system.saveStudents();
                    System.out.println("程序退出。");
                    System.exit(0);
                default:
                    System.out.println("无效的操作编号，请重新输入。");
            }
        }
    }
}
